//
//  HttpForAgent.m
//  yixintong
//
//  Created by longziyuan on 15-6-10.
//
//

#import "HttpForAgent.h"

@implementation HttpForAgent

-(void)loadData:(NSString *)url setData:(NSMutableData *)data setUsername:(NSString *)userName
{
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:ADVERTISEMENTURL]];
    [request setDelegate:self];
    [request setTimeOutSeconds:TIME_OUT_SECONDS];
    [request setUsername:userName];
    //nsStrRequestType=userName;
    [request setPostBody:data];
    if ([GlobalVar sharedInstance].blnUseProxy==YES)
    {
        [request setProxyPort:ProxyPort];
        [request setProxyHost:[GlobalVar getTheBestIp]];//
    }
    [request setDidFinishSelector:@selector(requestDataSuccess:)];
    [request setDidFailSelector:@selector(requestError:)];
    [request startAsynchronous];
}
//请求各种广告数据
-(void)requestData:(NSString *)adtype setUsername:(NSString *)userName
{
    NSString* mobileNum=[[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_KEY"];
    // NSLog(@"mobileNum======:%@",mobileNum);
    NSString* CanShu=[NSString stringWithFormat:@"<root><adtype>%@</adtype><cert>%@</cert><productid>%@</productid><platform>iphone</platform><mobile>%@</mobile></root>",adtype,[GlobalVar getTheCertNum],SOFT_ID,mobileNum];
    NSMutableData *data =[NSMutableData dataWithData:[CanShu dataUsingEncoding:NSUTF8StringEncoding]];
    [self loadData:ADVERTISEMENTURL setData:data setUsername:userName];
    // NSLog(@"请求各种广告数据=====%@",adtype);
}

//请求回来的内容
-(void)requestDataSuccess:(ASIHTTPRequest*)request
{
    NSString* str=[request responseString];
    //NSLog(@"请求回来的内容=request.username=======%@ == %@",request.username ,str);
    if ([request.username isEqualToString:GetAdv])
    {
        if ([_delegate respondsToSelector:@selector(requestAdvSuccess:)]) {
            [_delegate requestAdvSuccess:str];
        }
       
    }else if([request.username isEqualToString:GetLaunchView])//
    {
        if ([_delegate respondsToSelector:@selector(requestLuanchViewSuccess:)]) {
            [_delegate requestLuanchViewSuccess:str];
        }
    }else if([request.username isEqualToString:GetQueryPri])
    {
        if ([_delegate respondsToSelector:@selector(requestQueryPriSuccess:)]) {
            [_delegate requestQueryPriSuccess:str];
        }
        
    }
}

//获取失败
-(void)requestError:(ASIHTTPRequest*)request
{
    NSLog(@"获取失败=username=%@  Error====%@",request.username,[request error]);
}



@end
